import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "./plugins/element.js";
import "styles/normalize.sass";
import "styles/base.sass";
import "styles/filter.sass";
import "styles/dialog.sass";
import "styles/edit.sass";
import 'styles/element-variables.scss'

import moment from "moment";
Vue.prototype.$moment = function(date) {
  if (date) {
    return moment(date);
  } else {
    return {
      format: function() {
        return "--";
      }
    };
  }
};

import ElementUI from 'element-ui'
import './element-variables.scss'
Vue.use(ElementUI)

// 拖拽功能
import draggable from "vuedraggable";
Vue.use(draggable);

//注册插件
import GlobalComponentPlugins from '@/plugins/common'
Vue.use(GlobalComponentPlugins)

// 右键菜单
// import Contextmenu from "vue-contextmenujs"
// Vue.use(Contextmenu);

//无限滚动
// import infiniteScroll from "vue-infinite-scroll"
// Vue.use(infiniteScroll)

Vue.directive('loadmore', {
  bind (el, binding) {
    var p = 0;
    var t = 0;
    var down = true;
    var selectWrap = el.querySelector('.el-table__body-wrapper')
    selectWrap.addEventListener('scroll', function () {
      //判断是否向下滚动
      p = this.scrollTop;
      if (t < p) {
        down = true;
      } else {
        down = false;
      }
      t = p;
      //判断是否到底
      const sign = 3;
      const scrollDistance = this.scrollHeight - this.scrollTop - this.clientHeight
      if (scrollDistance <= sign && down) {
        binding.value()
      }
    })
  }
});

// 事件车
Vue.prototype.$bus = new Vue();


// 时限本地缓存
import Storage from "jss/ls";
const ls = new Storage();
Vue.prototype.$ls = ls;

import * as math from "mathjs";
Vue.prototype.$math = math;

Vue.config.productionTip = true;


const userInfo = ls.get("mujinguserInfo");
const token = ls.get("mujingtoken");
const permissionList = ls.get("mujingpermissionList");
const permissionNameList = ls.get("mujingpermissionNameList");
if (userInfo && token) {
  store.commit("saveUserInfo", userInfo);
  store.commit("saveToken", token);
  store.commit("savePermissionList", permissionList);
  store.commit("savePermissionNameList", permissionNameList);
} else {
  ls.remove("mujinguserInfo");
  ls.remove("mujingtoken");
  ls.remove("mujingpermissionList");
  ls.remove("mujingpermissionNameList");
  // location.reload();
}
import { Message } from "element-ui";
// import directive from './assets/js/directive'
// Vue.use(directive)
import './assets/js/drag.js';

import WebSocket from 'websocket';

router.beforeEach((to, from, next) => {
  // const token = ls.get("mujingtoken");
  const token = store.state.token;
  if (!to.meta.noAuth && !token) {
    // location.reload();
    Message({
      type: "warning",
      message: "请登录"
    });
    next({
      path: "/login"
    });
    return;
  }
  if (!!to.meta.noAuth && !!token) {
    console.log(to, 2222);
  // if (to.meta.noAuth && token) {
    next({
      path: "/homePage"
    });
    return;
  }
  next();
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
